﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Sales;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_KIT : RibbonForm
    {
        public M_KIT()
        {
            InitializeComponent();
        }

        void DisableComponents()
        {
            txtBOMCode.Enabled = false;
            txtBOMName.Enabled = false;
            txtItemCode.Enabled = false;
            txtItemName.Enabled = false;
            txtQty.Enabled = false;
            txtUoM.Enabled = false;

            btnAdd.Text = "Update";

        }

        void RemoveBlankLastRowFirst()
        {
            int iCount = -1;
            gridView1.MoveLast();

            int[] oRow = gridView1.GetSelectedRows();

            iCount = oRow[0];
            string sItemCode = "";


            sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();

            if (sItemCode.Trim() == "")
            {
                gridView1.DeleteRow(iCount);
            }


        }


        DataTable GetLineItems()
        {
            DataTable _table = new DataTable();

            _table.Columns.Add("ItemGroup", typeof(string));
            _table.Columns.Add("ItemCode", typeof(string));
            _table.Columns.Add("ItemName", typeof(string));
            _table.Columns.Add("Qty", typeof(double));
            _table.Columns.Add("UoM", typeof(string));
            _table.Columns.Add("IsFree", typeof(bool));

            return _table;
        }
        public string ConnectionString { get; set; }
        public DataTable LineItems { get; set; }

        private void M_BOM_Load(object sender, EventArgs e)
        {
            
            this.LineItems = GetLineItems();

            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();

            if (gridView1.DataRowCount <= 0)
            {
                gridView1.AddNewRow();
            }


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "Add")
            {
                KitManager oManager = new KitManager();
                KitUnit oUnit = new KitUnit();

                RemoveBlankLastRowFirst();
                oManager.ConnectionString = this.ConnectionString;

                oUnit.BOMCode = txtBOMCode.Text;
                oUnit.BOMName = txtBOMName.Text;
                oUnit.ItemCode = txtItemCode.Text;
                oUnit.ItemName = txtItemName.Text;
                oUnit.Qty = txtQty.Text;
                oUnit.UoM = txtUoM.Text;

                if (radioGroup1.SelectedIndex == 0)
                {
                    oUnit.IsSales = true;
                    oUnit.IsProduction = false;
                }
                else
                {
                    oUnit.IsSales = false;
                    oUnit.IsProduction = true;
                }

                oUnit.LineItems = this.LineItems;

                oManager.Open();
                oManager.AddBOM(oUnit);
                oManager.Close();

                txtBOMCode.Enabled = false;
                btnAdd.Text = "Update";
                radioGroup1.Enabled = false;
                txtItemCode.Properties.Buttons[0].Visible = false;

                MessageBox.Show("BOM is already added to the database", "BOM", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                KitManager oManager = new KitManager();
                KitUnit oUnit = new KitUnit();

                RemoveBlankLastRowFirst();
                oManager.ConnectionString = this.ConnectionString;

                oUnit.BOMCode = txtBOMCode.Text;
                oUnit.BOMName = txtBOMName.Text;
                oUnit.ItemCode = txtItemCode.Text;
                oUnit.ItemName = txtItemName.Text;
                oUnit.Qty = txtQty.Text;
                oUnit.UoM = txtUoM.Text;

                if (radioGroup1.SelectedIndex == 0)
                {
                    oUnit.IsSales = true;
                    oUnit.IsProduction = false;
                }
                else
                {
                    oUnit.IsSales = false;
                    oUnit.IsProduction = true;
                }

                oUnit.LineItems = this.LineItems;

                oManager.Open();
                oManager.UpdateBOM(oUnit);
                oManager.Close();

                MessageBox.Show("BOM is already updated to the database", "BOM", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtSearchItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_ITM_BOM oForm = new FND_ITM_BOM();
            int iFocusRow = gridView1.FocusedRowHandle;
            oForm.ConnectionString = this.ConnectionString;
            oForm.ItemGroup = gridView1.GetRowCellValue(iFocusRow, gridView1.Columns["ItemGroup"]).ToString();
            oForm.ShowDialog();

            int iRowCount = gridView1.FocusedRowHandle;

            gridView1.SetRowCellValue(iRowCount, gridView1.Columns["ItemCode"], oForm.SelectedItemCode);
            gridView1.SetRowCellValue(iRowCount, gridView1.Columns["ItemName"], oForm.SelectedItemDescription);
            gridView1.SetRowCellValue(iRowCount, gridView1.Columns["UoM"], oForm.SelectedItemUnitMeasure);
            gridView1.SetRowCellValue(iRowCount, gridView1.Columns["Qty"], "0");
            
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                int iCount = -1;
                int[] oRow = gridView1.GetSelectedRows();

                iCount = oRow[0];

                if (e.KeyCode == Keys.Delete)
                {
                    if (MessageBox.Show("Are you sure you want to delete this Row?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (gridView1.IsLastRow)
                        {
                            string sItemCode = "";

                            oRow = gridView1.GetSelectedRows();
                            iCount = oRow[0];
                            sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();
                            List<DataRow> lRows = new List<DataRow>();

                            foreach (DataRow _Row in this.LineItems.Rows)
                            {
                                if (_Row["ItemCode"].ToString() == sItemCode)
                                {
                                    lRows.Add(_Row);
                                }
                            }

                            foreach (DataRow delRow in lRows)
                            {
                                this.LineItems.Rows.Remove(delRow);
                            }

                            if (sItemCode != "")
                            {
                                gridView1.DeleteRow(iCount);
                                gridView1.AddNewRow();
                            }
                        }
                        else
                        {
                            string sItemCode = "";

                            oRow = gridView1.GetSelectedRows();
                            iCount = oRow[0];
                            sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();

                            if (sItemCode != "")
                            {
                                gridView1.DeleteRow(iCount);
                            }

                            gridView1.RefreshData();
                        }

                        
                    }
                }

                if (e.KeyCode == Keys.Down)
                {
                    if (gridView1.IsLastRow)
                    {
                        string sItemCode = "";
                        oRow = gridView1.GetSelectedRows();

                        iCount = oRow[0];
                        sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();

                        
                        gridView1.AddNewRow();
                        
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void txtItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                FND_ITM_SALES_ONLY oForm = new FND_ITM_SALES_ONLY();
                oForm.ConnectionString = this.ConnectionString;
                oForm.ShowDialog();
                if (oForm.SelectedItemCode != null)
                {
                    txtItemCode.Text = oForm.SelectedItemCode;
                    txtItemName.Text = oForm.SelectedItemName;
                    txtUoM.Text = oForm.SelectedUoM;

                    
                }
            }
            else
            {
                FND_ITM_PRD_ONLY oForm = new FND_ITM_PRD_ONLY();
                oForm.ConnectionString = this.ConnectionString;
                oForm.ShowDialog();
                if (oForm.SelectedItemCode != null)
                {
                    txtItemCode.Text = oForm.SelectedItemCode;
                    txtItemName.Text = oForm.SelectedItemName;
                    txtUoM.Text = oForm.SelectedUoM;
                }
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtItemName.Text = "";
            txtItemCode.Text = "";
            txtUoM.Text = "";
            txtQty.Text = "1";

            this.LineItems = new DataTable();
            this.LineItems = GetLineItems();

            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();


            if (gridView1.DataRowCount <= 0)
            {
                gridView1.AddNewRow();
            }
        }

        public void ShowSearch()
        {
            FND_KIT oForm = new FND_KIT();
            
            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            if (oForm.SelectedBOM != null)
            {
                txtBOMCode.Text = oForm.SelectedBOM;
                txtBOMName.Text = oForm.SelectedBOMName;

                KitManager oManager = new KitManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                DataTable _table = oManager.GetBOMByCode(txtBOMCode.Text);
                DataRow oRow = _table.Rows[0];
                txtBOMCode.Enabled = false;
                if ((bool)oRow["IsProduction"])
                {
                    radioGroup1.SelectedIndex = 1;
                }
                else
                {
                    radioGroup1.SelectedIndex = 0;
                }

                txtItemCode.Text = oRow["ItemCode"].ToString();
                txtItemName.Text = oRow["ItemName"].ToString();
                txtQty.Text = oRow["Qty"].ToString();
                txtUoM.Text = oRow["UoM"].ToString();

             
                this.LineItems = oManager.GetLineItems(txtBOMCode.Text);
                gridControl1.DataSource = this.LineItems;
                gridControl1.Refresh();

                if (gridView1.RowCount == 0)
                {
                    gridView1.AddNewRow();
                }

                oManager.Close();

                btnAdd.Text = "Update";
                radioGroup1.Enabled = false;
                txtItemCode.Properties.Buttons[0].Visible = false;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ShowSearch();
        }

        private void txtSearchItemCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                #region New Code From Sales Order

                string _ItemGUID = System.Guid.NewGuid().ToString();
                DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

                try
                {
                    string sItemCode = oButtonEdit.Text;
                    ItemManager oManager = new ItemManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    DataTable dt = oManager.GetInventoryItemByCode(sItemCode);

                    oManager.Close();

                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        #region If Has Rows

                        //_table.Columns.Add("ItemGroup", typeof(string));
                        //_table.Columns.Add("ItemCode", typeof(string));
                        //_table.Columns.Add("ItemName", typeof(string));
                        //_table.Columns.Add("Qty", typeof(double));
                        //_table.Columns.Add("UoM", typeof(string));
                        //_table.Columns.Add("IsFree", typeof(bool));


                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemCode"], row["ItemCode"].ToString());
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemName"], row["Description"].ToString());
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["UoM"], row["InvtyUntMsr"].ToString());

                        #endregion

                        gridView1.FocusedColumn = gridView1.Columns["Qty"];
                        gridView1.ShowEditor();

                        return;

                    }
                    else
                    {
                        if (sItemCode.Trim() != "")
                        {
                            MessageBox.Show("No item found in the Item Master File!!!", "Item Code", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            gridView1.FocusedColumn = gridView1.Columns["ItemCode"];

                            SendKeys.Send("{ENTER}");

                            return;
                        }

                    }
                }
                catch
                {
                }
                #endregion
            }


            if (e.KeyCode == Keys.Tab)
            {
                #region New Code From Sales Order
                
                string _ItemGUID = System.Guid.NewGuid().ToString();
                DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

                try
                {
                    string sItemCode = oButtonEdit.Text;
                    ItemManager oManager = new ItemManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    DataTable dt = oManager.GetInventoryItemByCode(sItemCode);

                    oManager.Close();

                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        #region If Has Rows

                        //_table.Columns.Add("ItemGroup", typeof(string));
                        //_table.Columns.Add("ItemCode", typeof(string));
                        //_table.Columns.Add("ItemName", typeof(string));
                        //_table.Columns.Add("Qty", typeof(double));
                        //_table.Columns.Add("UoM", typeof(string));
                        //_table.Columns.Add("IsFree", typeof(bool));


                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemCode"], row["ItemCode"].ToString());
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemName"], row["Description"].ToString());
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["UoM"], row["InvtyUntMsr"].ToString());
                      
                        #endregion

                        gridView1.FocusedColumn = gridView1.Columns["Qty"];
                        gridView1.ShowEditor();
                        
                        return;

                    }
                    else
                    {
                        if (sItemCode.Trim() != "")
                        {
                            MessageBox.Show("No item found in the item Master File!!!", "Item Code", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            gridView1.FocusedColumn = gridView1.Columns["ItemCode"];

                            SendKeys.Send("{ENTER}");

                            return;
                        }
                       
                    }
                }
                catch
                {
                }
                #endregion
            }
        }
    }
}
